//
//  SVAccountViewController.m
//  ShortVideo-ccinon
//
//  Created by LIN TONG on 2017/6/26.
//  Copyright © 2017年 LIN TONG. All rights reserved.
//

#import "SVAccountViewController.h"
#import "SVNavigationBar.h"
#import "SVConfig.h"
#import "QRCodeUtil.h"
#import "SVAPIReqeust.h"

@interface SVAccountViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *contentTable;
    NSMutableArray *groupDatas;
    NSMutableArray *iconTitles;
}

@end

@implementation SVAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    groupDatas = [NSMutableArray arrayWithArray:@[@[@"数据统计",@"敬请期待"],@[@"设置",@"联系开发商"],@[@"重新登录"]]];
    iconTitles = [NSMutableArray arrayWithArray:@[@[@"icon_statistics",@"icon_waitting"],@[@"icon_settings",@"icon_contact"]]];
    
    SVNavigationBar *naviBar = [[SVNavigationBar alloc]init];
    [naviBar setQrcodeHandler:^{
        [QRCodeUtil pushQRCodeVC:self WithCompletionHandler:^(BOOL success, NSString *result) {
            
        }];
    }];
    [naviBar setBackHander:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [naviBar setHidenAccountButton:YES];
    [naviBar setTitle:@"个人中心"];
    naviBar.showBackButton = YES;
    [self.view addSubview:naviBar];
    
    contentTable = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(naviBar.frame), __SCREEN_WIDTH__, __SCREEN_HEIGHT__-CGRectGetMaxY(naviBar.frame)) style:UITableViewStyleGrouped];
    contentTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    contentTable.dataSource = self;
    contentTable.delegate = self;
    contentTable.scrollEnabled = NO;
    [self.view addSubview:contentTable];
    
    UILabel *copyright = [[UILabel alloc]initWithFrame:CGRectMake(0, __SCREEN_HEIGHT__-S(7)-S(15), __SCREEN_WIDTH__, S(15))];
    copyright.textAlignment = NSTextAlignmentCenter;
    copyright.font = Font(12);
    copyright.textColor = UIColorMake(249, 74, 0, 1);
    copyright.text = @"copyright@ccinon";
    [self.view addSubview:copyright];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

#pragma mark - table view delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionDatas = groupDatas[section];
    return  sectionDatas.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return groupDatas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return S(44);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return S(24);
    }else if(section == 1){
        return S(19);
    }else{
        return S(115);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"identifier%ld",indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell){
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *horline1 = [[UIView alloc]initWithFrame:CGRectMake(0, -1, __SCREEN_WIDTH__, 1)];
        horline1.backgroundColor = UIColorMake(191, 191, 191, 1);
        [cell addSubview:horline1];
        
        UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(S(10), S(7), S(29), S(29))];
        iconImage.tag = 1001;
        [cell addSubview:iconImage];
        
        UILabel *videoName = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(iconImage.frame)+S(10), S(12), S(200), S(20))];
        videoName.textColor = UIColorMake(30, 30, 30, 1);
        [videoName setFont:Font(15)];
        videoName.tag = 1002;
        [cell addSubview:videoName];
        
        UIView *horline = [[UIView alloc]initWithFrame:CGRectMake(0, S(44)-1, __SCREEN_WIDTH__, 1)];
        horline.backgroundColor = UIColorMake(191, 191, 191, 1);
        [cell addSubview:horline];
    }
    
    UIImageView *icon = [cell viewWithTag:1001];
    if(indexPath.section != 2){
        icon.image = [UIImage imageNamed:iconTitles[indexPath.section][indexPath.row]];
    }
    
    UILabel *name = [cell viewWithTag:1002];
    name.text = groupDatas[indexPath.section][indexPath.row];
    
    if(indexPath.section == 2){
        name.textAlignment = NSTextAlignmentCenter;
        name.font = FontM(17);
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 2 && indexPath.row == 0){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定是否退出登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *OK = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
            [SVAPIReqeust svRequestAPI:API(@"/mct/admin/me/logout") method:@"GET" parametors:@{} completionBlock:^(NSString *requestURL, BOOL success, NSDictionary *data, NSError *error) {
                if(success){
                    [APPDELEGATE showLoginVC];
                }
            }];
        }];
        
        UIAlertAction *Cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [alert dismissViewControllerAnimated:YES completion:nil];
        }];
        
        [alert addAction:Cancel];
        [alert addAction:OK];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
}

@end
